<?php
if($_SESSION['sisa']!=1){
	echo "Akses tidak diizinkan";
	exit();
}

$act = filter($_GET['act']);
$field = filter($_GET['field']);
$q = filter($_GET['q']);

if(empty($act)){	
?>		
					<!-- box / title -->
					<div class="title">
						<h5>Data Sisa Produksi</h5>								
						<div class="search global-nav">
							<form method="GET" class="form-search js-search-form" action="index.php?pages=sisa_prod" id="global-nav-search" style="width:350px">
								<input type="hidden" name="pages" value="sisa_prod">
								<select name="field" id="birthday_month" aria-label="Month" class="" >
									<option value="" <?php echo ($field=="")?"selected":"";?>>Cari Berdasarkan:</option>
									<option value="id_prod" <?php echo ($field=="id_prod")?"selected":"";?>>Kode Produksi</option>															
								</select>

				            	<label class="visuallyhidden" for="search-query">Search query</label>
				            	<input class="search-input" type="text" id="search-query" placeholder="Search" name="q" value="<?php echo $q;?>">
				            	<span class="search-icon js-search-action">
				              		<button type="submit" class="icon nav-search" tabindex="-1">
				              			<span class="visuallyhidden">Search</span>
				              		</button>
				            	</span>
				            	<input disabled="disabled" class="search-input search-hinting-input" type="text" id="search-query-hint" autocomplete="off" spellcheck="false">				              					              
							</form>
						</div>
					</div>
					<!-- end box / title -->
					<div class="table">										
						<table>
							<thead>
								<tr>
									<th class="selected"><input type="checkbox" class="checkall" /></th>
									<th class="action">No.</th>
									<th class="left">Kode Produksi</th>
									<th>Jumlah Sisa</th>									
								</tr>
							</thead>
							<tbody>
							<?php			

							$q2=$q;

							if($field=='id_prod'){
								$sqlFormula = mysql_query("SELECT id_produksi FROM tb_produksi WHERE kode_prod LIKE '%$q%'");
								$onoFormula = mysql_num_rows($sqlFormula);
								//echo "SELECT id_formula FROM tb_formula WHERE kode LIKE '%$q%'";
								while ($dataFormula = mysql_fetch_array($sqlFormula)) {
									$qx .= $dataFormula['id_produksi'].',';
								}

								if($onoFormula)
									$q2 = substr($qx, 0, -1);
								else
									$q2 = '999999999999999999999';

							}							
							
							$addQuery='';


							if(!empty($field)){
								if($field=='id_prod')
									$addQuery .= " WHERE $field IN ($q2) ";
								else
									$addQuery .= " WHERE $field LIKE '%$q2%' ";
							}

							$batas=10;
							$pg=(int) $_GET['pg'];
							if(empty($pg))
							{
								$posisi=0;
								$pg=1;
							}
							else
							{
								$posisi = ($pg-1) * $batas;
							}
							$sqlKode1 = mysql_query("SELECT * FROM tb_sisa_produksi $addQuery ORDER BY id_sisa DESC limit $posisi, $batas");
							
							$no=$posisi+1;
							while($dataKode = mysql_fetch_array($sqlKode1)){								

								$dataProduksi = mysql_fetch_array(mysql_query("SELECT kode_prod FROM tb_produksi WHERE id_produksi='$dataKode[id_prod]' LIMIT 1"));
							?>
								<tr>
									<td class="selected"><input type="checkbox" /></td>
									<td class="center"><?php echo $no;?></td>
									<td><?php echo $dataProduksi['kode_prod'];?></td>
									<td><?php echo $dataKode['jumlah_sisa'];?> Cup</td>																															
								</tr>
							<?php	
								$no++;				
							}

							$sqlSup2 = mysql_query("SELECT * FROM tb_sisa_produksi $addQuery ORDER BY id_sisa DESC");
							$jumlahData = mysql_num_rows($sqlSup2);

							$file="?pages=sisa_prod&field=$field&q=$q";
							$jmlhalaman=ceil($jumlahData/$batas);
	
							?>															
							</tbody>
						</table>
						<!-- pagination -->
						<div class="pagination pagination-left">
							<div class="results">
								<span><?php echo "Showing Results ".($posisi+1)."-".($no-1)." of $jumlahData";?></span>
							</div>
							<ul class="pager">
								<?php
								//link ke halaman sebelumnya (previous)
								if($pg > 1){
									$previous=$pg-1;
									echo "<li><a href=$file&pg=1>&laquo; First</a></li>
									<li><a href=$file&pg=$previous>&laquo; Prev</a></li> ";
								}else{ 
									echo "<li class='disabled'>&laquo; First</li>
									<li class='disabled'>&laquo; Prev</li> ";
								}
								?>

								<?php
								//Link untuk Angka
								$angka=($pg > 3 ? " <li class='separator'>...</li> " : " ");
								for($i=$pg-2;$i<$pg;$i++)
								{
								  if ($i < 1) 
								      continue;
								  $angka .= "<li><a href='$file&pg=$i'>$i</a></li> ";
								}

								$angka .= " <li class='current'>$pg</li>";
								for($i=$pg+1;$i<($pg+3);$i++)
								{
								  if ($i > $jmlhalaman) 
								      break;
								  $angka .= "<li><a href='$file&pg=$i'>$i</a></li> ";
								}

								$angka .= ($pg+2<$jmlhalaman ? " <li class='separator'>...</li> <li><a href='$file&pg=$jmlhalaman'>$jmlhalaman</a></li>" : " ");

								echo "$angka";
								?>
								

								<?php
								//link kehalaman berikutnya (Next)
								if($pg < $jmlhalaman)
								{
									$next=$pg+1;
									echo "<li><a href='$file&pg=$next'>Next &raquo;</a></li>
									<li><a href='$file&pg=$jmlhalaman'>Last &raquo;</a></li>";
								}
								else
								{ 
									echo "<li class='disabled'>Next &raquo;</li>
									<li class='disabled'>Last &raquo;</li> ";
								}		
								?>													
							</ul>
						</div>

						<!-- end pagination -->
						<!-- table action -->
						
						<!-- end table action -->	

						<div style="margin:30px auto">
							<?php
							$sqlTemp = mysql_query("SELECT * FROM tb_temp_rts LIMIT 1");
							$dataTemp = mysql_fetch_array($sqlTemp);
							?>
							<form method="POST" action="?pages=sisa_prod&act=update">
								<table style="width:auto">
								   	<tbody>
								   		<tr>
											<td><label><span class="left">Total Sisa Produksi</span></label></td>
											<td>:</td>
											<td><input type="text" value="<?php echo $dataTemp['stock_sisa'];?>" size="20" name="tot_sisa" id="tot_sisa" readonly style="background-color: #ffd76e"><span class="add-on"> Cup</span></td>
									   	</tr>	
									   	<tr>
											<td><label><span class="left">Packing ke RTS</span></label></td>
											<td>:</td>
											<td><input type="text"  size="20" name="karton" id="karton" required><span class="add-on"> Karton</span></td>											
											<td><label><span class="left">Isi Per Karton</span></label></td>
											<td>:</td>
											<td><input type="text"  size="20" name="cup" id="cup" required><span class="add-on"> Cup</span></td>
											<td><input type="submit" value="Simpan" name="submit" class="btn btn-primary"></td>											
									   	</tr>																   
									</tbody>
								</table>								
							</form>
						</div>					
					</div>

<?php
}elseif (isset($_POST['submit'])) {
	# code...
	$now = date('Y-m-d H:i:s');
	$karton = $_POST['karton'];
	$cup = $_POST['cup'];
	$tot_sisa = $_POST['tot_sisa'];

	$jmlCup = $karton * $cup;

	$sqlTemp = mysql_query("SELECT * FROM tb_temp_rts LIMIT 1");
	$dataTemp = mysql_fetch_array($sqlTemp);
	$stock_sisa = $dataTemp[stock_sisa];
	
	if($stock_sisa>=$jmlCup){

		$sqlInsert = mysql_query("INSERT INTO tb_rts(jumlah, satuan, status, status_asal, tgl) VALUES('$karton', 'Karton', '1', '2', '$now')");

		$tempRts = $dataTemp['stock_rts'] + $karton;
		$tempSisa = $dataTemp['stock_sisa'] - $jmlCup;

		$updateRts = mysql_query("UPDATE tb_temp_rts SET stock_rts  ='$tempRts', stock_sisa='$tempSisa' ");

		if($sqlInsert)
			sukses('Data berhasil disimpan..', '?pages=sisa_prod');

	}else{
		sukses('Total Sisa tidak mencukupi..', '?pages=sisa_prod');exit();
	}

}
?>					